package kr.hkit.powerjava.chapter17;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

import java.awt.GridLayout;

public class SwingComponentAll extends JFrame implements ItemListener,
		ActionListener {

	private JPanel contentPane;
	private JPanel panelImage;
	private JPanel panelChkBox;
	private JPanel panelRadioBtn;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private JPanel panelChk;
	private JRadioButton small;
	private JRadioButton medium;
	private JRadioButton large;
	private JLabel lblLabelBtn;
	private JPanel topPanel;
	private JPanel sizePanel;
	private JPanel resultPanel;
	private JLabel text;

	JCheckBox[] chkBtns = new JCheckBox[3];
	String[] strs = { "111", "222", "333" };
	JLabel[] lbls = new JLabel[3];
	ImageIcon[] icons = new ImageIcon[3];

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SwingComponentAll frame = new SwingComponentAll();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public SwingComponentAll() {
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

		panelImage = new JPanel();
		contentPane.add(panelImage);
		panelImage.setLayout(new BorderLayout(0, 0));

		lblNewLabel = new JLabel("이미지를 보려면 아래 버튼을 누르세요");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panelImage.add(lblNewLabel, BorderLayout.CENTER);

		btnNewButton = new JButton("이미지 레이블");
		btnNewButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (lblNewLabel.getIcon() == null) {
					ImageIcon icon = new ImageIcon("111.jpg");
					lblNewLabel.setIcon(icon);
					lblNewLabel.setText(null);
				} else {
					lblNewLabel.setIcon(null);
					lblNewLabel.setText("이미지를 보려면 아래 버튼을 누르세요");
				}
			}
		});
		panelImage.add(btnNewButton, BorderLayout.SOUTH);

		panelChkBox = new JPanel();
		contentPane.add(panelChkBox);
		panelChkBox.setLayout(new GridLayout(0, 4, 0, 0));

		panelChk = new JPanel(new GridLayout(0, 1));

		for (int i = 0; i < 3; i++) {
			chkBtns[i] = new JCheckBox(strs[i]);
			chkBtns[i].addItemListener(this);
			lbls[i] = new JLabel(strs[i] + ".jpg");
			icons[i] = new ImageIcon(strs[i] + ".jpg");

			panelChk.add(chkBtns[i]);

		}

		panelChkBox.add(panelChk);

		panelChkBox.add(lbls[0]);
		panelChkBox.add(lbls[1]);
		panelChkBox.add(lbls[2]);

		panelRadioBtn = new JPanel();
		contentPane.add(panelRadioBtn);
		panelRadioBtn.setLayout(new BorderLayout(0, 0));

		topPanel = new JPanel();
		panelRadioBtn.add(topPanel, BorderLayout.NORTH);

		lblLabelBtn = new JLabel("어떤 크기의 커피를 주문 하시겠습니까?");
		topPanel.add(lblLabelBtn);
		lblLabelBtn.setHorizontalAlignment(SwingConstants.CENTER);

		ButtonGroup size = new ButtonGroup();

		resultPanel = new JPanel();
		text = new JLabel("크기가 선택되지 않았습니다");
		text.setForeground(Color.RED);
		resultPanel.add(text);
		panelRadioBtn.add(resultPanel, BorderLayout.SOUTH);

		sizePanel = new JPanel();
		panelRadioBtn.add(sizePanel, BorderLayout.CENTER);
		// panelRadioBtn.add(sizePanel, BorderLayout.CENTER);
		Border border = BorderFactory.createTitledBorder("크기");
		sizePanel.setBorder(border);
		small = new JRadioButton("Small Size");
		small.addActionListener(this);
		sizePanel.add(small);

		medium = new JRadioButton("Medium Size");
		medium.addActionListener(this);
		sizePanel.add(medium);

		large = new JRadioButton("Large Size");
		large.addActionListener(this);
		sizePanel.add(large);
		size.add(small);
		size.add(medium);
		size.add(large);

		small.addActionListener(this);
		medium.addActionListener(this);
		large.addActionListener(this);

		sizePanel.add(small);
		sizePanel.add(medium);
		sizePanel.add(large);
		setVisible(true);

	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		ImageIcon image = null;

		Object source = e.getItemSelectable();
		for (int i = 0; i < 3; i++) {

			if (source == chkBtns[i]) {
				if (e.getStateChange() == ItemEvent.DESELECTED) {
					lbls[i].setIcon(null);
				} else {
					lbls[i].setIcon(icons[i]);
				}
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == small) {
			text.setText("Small 크기가 선택되었습니다.");
		} else if (e.getSource() == medium) {
			text.setText("Medium 크기가 선택되었습니다.");
		} else {
			text.setText("Large 크기가 선택되었습니다.");
		}
	}
}
